
package com.cmc.dao.hibernate;

import java.util.List;

import com.cmc.dao.hibernate.BaseDaoHibernate;
import com.cmc.model.CardTime;
import com.cmc.dao.CardTimeDao;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.hibernate3.HibernateCallback;

public class CardTimeDaoHibernate extends BaseDaoHibernate implements CardTimeDao {

    /**
     * @see com.cmc.dao.CardTimeDao#getCardTimes(com.cmc.model.CardTime)
     */
    public List getCardTimes(final CardTime cardTime) {
        //return getHibernateTemplate().find("from CardTime");

        /* Remove the line above and uncomment this code block if you want 
           to use Hibernate's Query by Example API. */
        if (cardTime == null) {
            return getHibernateTemplate().find("from CardTime");
        } else {
            // filter on properties set in the cardTime
            HibernateCallback callback = new HibernateCallback() {
                public Object doInHibernate(Session session) throws HibernateException {
                    Example ex = Example.create(cardTime).ignoreCase().enableLike(MatchMode.ANYWHERE);
                    return session.createCriteria(CardTime.class).add(ex).list();
                }
            };
            return (List) getHibernateTemplate().execute(callback);
        }
    }

    /**
     * @see com.cmc.dao.CardTimeDao#getCardTime(Long id)
     */
    public CardTime getCardTime(final Long id) {
        CardTime cardTime = (CardTime) getHibernateTemplate().get(CardTime.class, id);
        if (cardTime == null) {
            log.warn("uh oh, cardTime with id '" + id + "' not found...");
            throw new ObjectRetrievalFailureException(CardTime.class, id);
        }

        return cardTime;
    }

    /**
     * @see com.cmc.dao.CardTimeDao#saveCardTime(CardTime cardTime)
     */    
    public void saveCardTime(final CardTime cardTime) {
        getHibernateTemplate().saveOrUpdate(cardTime);
    }

    /**
     * @see com.cmc.dao.CardTimeDao#removeCardTime(Long id)
     */
    public void removeCardTime(final Long id) {
        getHibernateTemplate().delete(getCardTime(id));
    }
}
